<?php

namespace  addons\qingdong\library;

use addons\qingdong\model\AdminConfig;
use addons\qingdong\model\Contacts;
use addons\qingdong\model\Customer;
use addons\qingdong\model\KuCustomer;
use think\Model;
use fast\Http;

/**
 *
 */
class Ku extends Model
{

    //客户增加
    public function addCustomer($params=null){
        $ku = AdminConfig::where(['type'=>AdminConfig::TYPE_KU])->column('field,value');
        if(isset($ku) && $ku && $params){
            if($ku['status'] == 1 && $ku['account'] && $ku['key']){
                $info = KuCustomer::where(array('customer_id'=>$params['customer_id']))->find();
                if($info){
                    return true;
                }
                $customer = Customer::where(array('id'=>$params['customer_id']))->find();
                $url =$ku['url'].'api/kuerp/third/customers/joinCustomers';
                $sign = md5('account='.$ku['account'].'&mobile='.$params['mobile'].'&name='.$customer['name'].'key='.$ku['key']);
                $data = array(
                    'account'=>$ku['account'],
                    'sign'=>$sign,
                    'request_time'=>time(),
                    'name'=>$customer['name'],
                    'mobile'=>$params['mobile'],
                    'address'=>$customer['address'],
                    'note'=>$customer['remarks'],
                );
                $result = $this->http_post($url,$data);
                $result = json_decode($result,true);
                if($result['code'] == 1){
                    $ress = array(
                        'customer_id'=>$params['customer_id'],
                        'contacts_id'=>$params['contacts_id'],
                        'ku_id'=>$result['data']['id'],
                        'code'=>$result['data']['customer_code'],
                        'createtime'=>time()
                    );
                    KuCustomer::create($ress);
                }
            }
        }
        return true;
    }
    //客户修改
    public function editCustomer($params=null){

        $ku = AdminConfig::where(['type'=>AdminConfig::TYPE_KU])->column('field,value');
        if(isset($ku) && $ku && $params){
            if($ku['status'] == 1 && $ku['account'] && $ku['key']){
                $info = KuCustomer::where(array('customer_id'=>$params['id']))->find();
                if(!$info){
                    return true;
                }
                $contacts = Contacts::where(array('id'=>$info['contacts_id']))->value('mobile');
                $url =$ku['url'].'api/kuerp/third/customers/editCustomers';
                $sign = md5('account='.$ku['account'].'&code='.$info['code'].'&mobile='.$contacts.'&name='.$params['name'].'key='.$ku['key']);
                $data = array(
                    'account'=>$ku['account'],
                    'sign'=>$sign,
                    'request_time'=>time(),
                    'code'=>$info['code'],
                    'name'=>$params['name'],
                    'mobile'=>$contacts,
                    'address'=>$params['address'],
                );
                $result = $this->http_post($url,$data);
                $result = json_decode($result,true);
            }
        }
        return true;
    }
    public function http_post($url,$param,$post_file=false){
        $oCurl = curl_init();
        if(stripos($url,"https://")!==FALSE){
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        if (is_string($param) || $post_file) {
            $strPOST = $param;
        } else {
            $aPOST = array();
            foreach($param as $key=>$val){
                $aPOST[] = $key."=".urlencode($val);
            }
            $strPOST =  join("&", $aPOST);
        }
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt($oCurl, CURLOPT_POST,true);
        curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);
        if(intval($aStatus["http_code"])==200){
            return $sContent;
        }else{
            return false;
        }
    }

}